﻿using Microsoft.Extensions.Primitives;
using SendMailServer.FileMatch;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SendMailServer.Search
{
    internal class SearchMainContent
    {
        private string m_ContendDir { get; set; }
        public SearchMainContent() { }

        public ResMsg Search(string key, string filterStr)
        {
            try
            {
                if (key != "SearchMainContent")
                {
                    return null;
                }
                if (filterStr == null) { filterStr = ""; }
                var ks = filterStr.Split(',');
                var searchTxt = ks[0];
                var appName = ks[1];

                m_ContendDir = WSocketHandle.GetContendDir(appName);

                var searchFile = Path.Combine(m_ContendDir, "IndexSearch.txt");
                if (File.Exists(searchFile) == false)
                {
                    return new ResMsg()
                    {
                        MsgCode = key,
                        IsOk = true,
                        Content = "没有建立搜索索引",
                        UseSelfResponse = true
                    };
                }

                return new ResMsg()
                {
                    MsgCode = key,
                    IsOk = true,
                    Content = GetSearchFiles(searchFile, searchTxt, appName),
                    UseSelfResponse = true
                };
            }
            catch (Exception err)
            {
                return new ResMsg()
                {
                    MsgCode = key,
                    IsOk = false,
                    Content = err.Message+","+err.StackTrace,
                    UseSelfResponse = true
                };
            }
            
        }
        private string GetSearchFiles(string searchFile,string searchContent,string appName)
        {
            var fs = File.ReadAllText(searchFile);
            var lastT = 0;
            StringBuilder sb = new StringBuilder();
            while (true)
            {
                var t = fs.IndexOf(searchContent, lastT);
                if (t == -1)
                {
                    break;
                }
                var contentFileDom = "<ContentFile>";
                var fileNameIndex = fs.LastIndexOf(contentFileDom, t) + contentFileDom.Length;
                var fileNameIndexEnd = fs.IndexOf("</ContentFile>", fileNameIndex);
                var sf = fs.Substring(fileNameIndex, fileNameIndexEnd - fileNameIndex);
                var fileNameAndFileCode = sf.Split('&');
                if (sb.Length > 0)
                {
                    sb.Append(',');
                }
                sb.Append(fileNameAndFileCode[0]);
                var fileCode = fileNameAndFileCode[1];
                var fullFileName = FileCodeMatch.GetFileName(fileCode, appName,out string debuggerValue);
                if (fullFileName == null)
                {
                    fullFileName = "没有找到文件:" + fileCode+":"+ debuggerValue;
                }
                else
                {
                    var t3 = fullFileName.IndexOf(appName, StringComparison.InvariantCultureIgnoreCase);
                    var t4 = t3 > 0 ? fullFileName.LastIndexOf("\\", t3) : 0;
                    fullFileName = t4 > 0 ? fullFileName.Substring(t4) : "";
                }
               
                sb.Append(";"+fullFileName);
                var t2 = t - 10 < 0 ? 0 : t - 10;
                var minContent = t2 + 60 < fs.Length ? fs.Substring(t2, 60) : fs.Substring(t2);
                Regex.Replace(minContent, ",", "");
                Regex.Replace(minContent, ";", "");

                sb.Append(";" + minContent);
                lastT = fs.IndexOf(contentFileDom, t);
                if (lastT == -1) break;
            }
            return sb.ToString();
        }
    }
}
